﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYTXL.AppCode;
using System.Data;
using XYTXL.Data;

namespace XYTXL.View.Comon
{
    public partial class CommonUpdate : BasePage
    {


        public string strFormCode
        {

            get { return Request.QueryString["FormCode"] ?? ""; }
        }
        public string strFormID
        {

            get { return Request.QueryString["FormID"] ?? "0"; }
        }
        public FormUControl WFForm
        {

            get { return UserControl.Controls[0] as FormUControl; }
        }
        public JH_Auth_FormBase FormBaseData
        {
            get
            {
                return ViewState[hidFormCode.Value] == null ? new FormHelp().GetFormCodeByFormCode(hidFormCode.Value) : (JH_Auth_FormBase)ViewState[hidFormCode.Value];
            }
            set
            {
                ViewState[hidFormCode.Value] = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            try
            {
                hidFormCode.Value = strFormCode;
                hidFormID.Value = strFormID;
                //加载用户控件
                FormUControl Control = LoadControl("/View/UCMangForm/" + FormBaseData.FormMangerUrl) as FormUControl;
                UserControl.Controls.Add(Control);
                base.OnInit(e);
                //为用户控件加载用户数据
                Control.UserInfo = UserInfo;
                lblTitle.Text = FormBaseData.FormDec;
                if (!IsPostBack)
                {
                    //初始化添加表单
                    Control.initALLForm(hidFormCode.Value);
                    Control.initMangerForm(int.Parse(strFormID), "UPDATE");


                }
            }
            catch (Exception)
            {

                throw;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btUpdateForm_Click(object sender, EventArgs e)
        {
            string strVerifyMsg = WFForm.VerifyUpdateForm(int.Parse(strFormID));
            if (strVerifyMsg == "")
            {
                FormHelp FormHelp = new FormHelp();
                //为表单添加数据
                try
                {

                    //执行表单保存数据方法
                    if (WFForm.SaveForm(int.Parse(strFormID), "UPDATE"))
                    {
                        btUpdateForm.Enabled = false;
                        AlertMSG("3", "操作成功");
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscriptsuc", "<script>AddSuccess()</script>");
                    }
                }
                catch (Exception ex)
                {
                    AlertMSG("1", ex.Message.ToString());
                }

            }
            else
            {
                AlertMSG("1", strVerifyMsg);
            }


        }
        protected void btCancelForm_Click(object sender, EventArgs e)
        {
            FormHelp FormHelp = new FormHelp();
            FormHelp.DelForm(int.Parse(strFormID), FormBaseData.FormTable);
            AlertMSG("3", "操作成功");
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscriptsuc", "<script>AddSuccess()</script>");
            btUpdateForm.Enabled = false;
            btCancelForm.Enabled = false;

        }
    }
}